unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, MemDS, DBAccess, Uni,
  Vcl.ToolWin, Vcl.ComCtrls, Vcl.ActnList, Vcl.ButtonGroup, Vcl.ExtCtrls,
  RzPanel, RzGroupBar, RzButton;

type
  Tfrmmain = class(TForm)
    rzstsbr1: TRzStatusBar;
    rzgrpbr1: TRzGroupBar;
    tlbfirst: TRzToolbar;
    btn1: TRzToolButton;
    rzgrp1: TRzGroup;
    procedure tbdepClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    procedure firstbtnclick(sender:TObject);
    procedure FillGroupItem(group:TRzGroup;pid:Integer);
    procedure GroupItemClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  frmmain: Tfrmmain;

implementation
uses
  depart,userlist,dm,module,base,System.StrUtils;

{$R *.dfm}

procedure Tfrmmain.FillGroupItem(group:TRzGroup;pid: Integer);
var
  btn:TRzGroupItem;
  ps:PString;
begin
  DataModule1.unimodule.Filtered:=false;
  DataModule1.unimodule.Filter:='parentid='+inttostr(pid);
  DataModule1.unimodule.Filtered:=true;
  DataModule1.unimodule.First;
  group.Items.BeginUpdate;
  while not DataModule1.unimodule.Eof do
  begin
  btn:=group.Items.Add;
  btn.Caption:=DataModule1.unimodule.FieldByName('modulename').AsString;
  btn.Tag:=DataModule1.unimodule.FieldByName('moduleid').AsInteger;
  New(ps);
  ps^:=DataModule1.unimodule.FieldByName('url').AsString;
  btn.Data:=ps;
  btn.OnClick:=GroupItemClick;
  DataModule1.unimodule.Next;
  end;
  group.Items.EndUpdate;
end;

procedure Tfrmmain.firstbtnclick(sender: TObject);
var
  group:TRzGroup;
  pid:integer;
  n:Integer;
  i: Integer;
begin
  pid:=TRzToolButton(sender).tag;
  DataModule1.unimodule.Filtered:=false;
  DataModule1.unimodule.Filter:='parentid='+inttostr(pid);
  DataModule1.unimodule.Filtered:=true;
  DataModule1.unimodule.First;
  while rzgrpbr1.GroupCount>0 do
  begin
    rzgrpbr1.Groups[0].Free;
  end;
  while not DataModule1.unimodule.Eof do
  begin
    group:=TRzGroup.Create(rzgrpbr1);
    rzgrpbr1.AddGroup(group);
    group.Caption:=DataModule1.unimodule.FieldByName('modulename').AsString;
    group.Tag:=DataModule1.unimodule.FieldByName('moduleid').AsInteger;
    DataModule1.unimodule.Next;
  end;
  n:=rzgrpbr1.GroupCount;
  for i := 0 to n-1 do
  begin
    FillGroupItem(rzgrpbr1.Groups[i],rzgrpbr1.Groups[i].tag);
  end;
end;

procedure Tfrmmain.FormShow(Sender: TObject);
var
  frm:tfrmmodule;
  btn:TRzToolButton;
begin
  while tlbfirst.ControlCount>0 do
  begin
    tlbfirst.Controls[0].Free;
  end;
  while rzgrpbr1.GroupCount>0 do
  begin
    rzgrpbr1.Groups[0].Free;
  end;
  datamodule1.unimodule.Active:=true;
  if DataModule1.unimodule.RecordCount=0 then
  begin
    frm:=Tfrmmodule.Create(self);
    frm.ShowModal;
  end;
  DataModule1.unimodule.Filtered:=false;
  DataModule1.unimodule.Filter:='parentid=0';
  DataModule1.unimodule.Filtered:=true;
  DataModule1.unimodule.First;
  while not DataModule1.unimodule.Eof do
  begin
  btn:=TRzToolButton.Create(tlbfirst);
  btn.Parent:=tlbfirst;
  btn.Caption:=DataModule1.unimodule.FieldByName('modulename').AsString;
  btn.onclick:=firstbtnclick;
  btn.Tag:=DataModule1.unimodule.FieldByName('moduleid').AsInteger;
  DataModule1.unimodule.Next;
  end;
  SetFocus;
end;

procedure Tfrmmain.GroupItemClick(Sender: TObject);
var
 frm:tfrmbase;
 s,mname,param:string;
 n:integer;
begin
  s:=pstring(TRzGroupItem(Sender).Data)^;
  if s<>'' then
  begin
    n:=Pos('?',s);
    if n>0 then
    begin
      mname:=LeftStr(PChar(s),n-1);
      param:=MidStr(s,n+1,Length(s));
    end
    else
      mname:=s;
  //Application.CreateForm(cl,frm);
  frm:=tfrmbaseclass(GetClass(mname)).Create(self);
  frm.moduleid:=TRzGroupItem(Sender).Tag;
  frm.RetriveParam(param);
  frm.ShowModal;
  //frm.Free;
  end;
end;

procedure Tfrmmain.tbdepClick(Sender: TObject);
var
  frm:Tfrmdepart;
begin
  frm:=Tfrmdepart.create(self);
  frm.show;
end;

end.
